package com.baihang.framework.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.baihang.framework.entity.BaseEntity;
import com.baihang.framework.model.BaseModel;

@Component
public class BaseDao {
	private static Logger logger = LoggerFactory.getLogger(BaseDao.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public BaseDao() {};

	public BaseDao(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public JdbcTemplate getJdbcTemplate() {
		return this.jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public int save(String saveSql) {
		// System.err.println(saveSql);
		int result = jdbcTemplate.update(saveSql);

		return result;
	}

	public int delete(String sql) {
		int result=0;
		try {
			result = jdbcTemplate.update(sql);
		}catch(Exception ex) {
			logger.info("删除失败");
		}
		return result;
	}

	public int update(String sql) {
		int result = jdbcTemplate.update(sql);
		return result;
	}

	public int queryForInt(String sql) {
		int result = 0;
		try {
			result = jdbcTemplate.queryForObject(sql, int.class);		
		}catch (Exception e) {
			logger.info("queryForInt查询异常");
			return 0;
		}
		return result;
	}

	public Long queryForLong(String sql) {
		Long result = jdbcTemplate.queryForObject(sql, Long.class);
		return result;
	}

	public String queryForString(String sql) {
		String result = jdbcTemplate.queryForObject(sql, String.class);
		return result;
	}

	public Map<String, Object> queryForMap(String sql) {
		Map<String, Object> result = jdbcTemplate.queryForMap(sql);
		return result;
	}
	
	public List<String> queryForListByString(String sql){
		return jdbcTemplate.queryForList(sql, String.class);
	}
	public List<Map<String, Object>> queryForList(String sql) {
		List<Map<String, Object>> result = jdbcTemplate.queryForList(sql);
		return result;
	}

	public BaseModel queryForModel(String sql, RowMapper<BaseModel> rowMapper) {
		BaseModel model = null;
		try {
			model = (BaseModel) jdbcTemplate.queryForObject(sql, rowMapper);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return model;
	}

	public List<BaseModel> queryForModelList(String sql, RowMapper<BaseModel> rowMapper) {
		List<BaseModel> modelList = null;
		try {
			modelList = jdbcTemplate.query(sql, rowMapper);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return modelList;
	}
	
	public BaseEntity queryForEntity(String sql, RowMapper<BaseEntity> rowMapper) {
		BaseEntity entity = null;
		try {
			entity = (BaseEntity) jdbcTemplate.queryForObject(sql, rowMapper);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return entity;
	}

	public List<BaseEntity> queryForEntityList(String sql, RowMapper<BaseEntity> rowMapper) {
		List<BaseEntity> EntityList = null;
		try {
			EntityList = jdbcTemplate.query(sql, rowMapper);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return EntityList;
	}
	
	public Record queryForRecord(String sql) {
		Map<String, Object> result =null;
		Record record =null;
		try {
		 result = jdbcTemplate.queryForMap(sql);
		 record = new Record(result);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return record;
	}

	public List<Record> queryForRecordList(String sql) {
		List<Map<String, Object>> result = jdbcTemplate.queryForList(sql);
		// System.err.println(result.size());
		List<Record> recordList = new ArrayList<Record>();
		for (Map<String, Object> map : result) {
			// System.err.println((String)map.get("D_BanName"));
			recordList.add(new Record(map));
		}
		return recordList;
	}

	/**
	 * 根据库名和表名 查询列
	 * 
	 * @param table_schema
	 * @param table_name
	 * @return
	 */
	public List<Record> queryForTableColumnName(String table_schema, String table_name) {
		String sql = String.format(
				"select column_name from information_schema.columns where table_schema ='%s' and table_name = '%s'",
				table_schema, table_name);
		List<Map<String, Object>> result = jdbcTemplate.queryForList(sql);
		// System.err.println(result.size());
		List<Record> recordList = new ArrayList<Record>();
		for (Map<String, Object> map : result) {
			// System.err.println((String)map.get("D_BanName"));
			recordList.add(new Record(map));
		}
		return recordList;
	}

}
